package com.stox.client.ui.javafx.widget;

import javafx.collections.ListChangeListener;
import javafx.scene.Node;
import javafx.scene.layout.HBox;

import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;

public class SegmentedButtonBar extends HBox implements ListChangeListener<Node>{

	public SegmentedButtonBar(Node... children) {
		super(children);
		UI.style(this, CSS.segmentedButtonBar);
		getChildren().addListener(this);
	}

	@Override
	public void onChanged(ListChangeListener.Change<? extends Node> change) {
		while(change.next()){
			if(!getChildren().isEmpty()){
				Node node = null;
				for(int index = 1; index < getChildren().size() - 1; index++){
					node = getChildren().get(index);
					node.getStyleClass().removeAll(CSS.segmentedFirst, CSS.segmentedLast);
					if(!node.getStyleClass().contains(CSS.segmentedNode)){
						UI.style(node, CSS.segmentedNode);
					}
				}
				node = getChildren().get(0);
				if(!node.getStyleClass().contains(CSS.segmentedFirst)){
					UI.style(node, CSS.segmentedFirst);
				}
				node = getChildren().get(getChildren().size() - 1);
				if(!node.getStyleClass().contains(CSS.segmentedLast)){
					UI.style(node, CSS.segmentedLast);
				}
			}
		}
	}
	
}
